JavaScript Strings
In JavaScript, strings are used to represent textual data. They are sequences of characters enclosed within either single quotes (') or double quotes (").
String Methods:
JavaScript provides a variety of built-in methods to manipulate strings:
Length: Returns the length of the string.
Example
console.log(str1.length); // Output: 13You can click on above box to edit the code and run again.
Output
charAt(index): Returns the character at the specified index.
Example
console.log(str1.charAt(0)); // Output: H
concat(str1, str2, ...): Concatenates two or more strings.
Example
let fullName = str1.concat(' ', str2); console.log(fullName); // Output: Hello, World! JavaScript
indexOf(substring): Returns the index within the calling string of the first occurrence of the specified substring.
Example
console.log(str1.indexOf('World')); // Output: 7
slice(start, end): Extracts a section of a string and returns it as a new string.
Example
console.log(str1.slice(0, 5)); // Output: Hello
toUpperCase(): Converts the string to uppercase.
Example
console.log(str2.toUpperCase()); // Output: JAVASCRIPT
toLowerCase(): Converts the string to lowercase.
Example
console.log(str2.toLowerCase()); // Output: javascript
split(separator): Splits a string into an array of substrings based on a specified separator.